<template>
  <div class="columns py-6">
    <div class="column is-half is-offset-one-quarter">
      <el-card shadow="never">
        <div slot="header" class="has-text-centered has-text-weight-bold">
          {{ $t('Register.newOne') }}
        </div>
        <div>
          <el-form
            ref="ruleForm"
            v-loading="loading"
            :model="ruleForm"
            status-icon
            :rules="rules"
            :label-width="$i18n.locale === 'zh' ? '100px': '155px'"
            class="demo-ruleForm"
          >
            <el-form-item :label="$t('Register.account')" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>

            <el-form-item :label="$t('Register.password')" prop="password">
              <el-input
                v-model="ruleForm.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>

            <el-form-item :label="$t('Register.password2')" prop="checkPassword">
              <el-input
                v-model="ruleForm.checkPassword"
                type="password"
                autocomplete="off"
              />
            </el-form-item>

            <el-form-item :label="$t('Register.email')" prop="email">
              <el-input v-model="ruleForm.email" autocomplete="off" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">
                {{ $t('Register.signUp') }}
              </el-button>
              <el-button type="primary" @click="reSetForm('ruleForm')">
                {{ $t('Register.reset') }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { register } from "@/api/auth/auth";
export default {
  name: "Register",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error(this.$t('Register.rePass')));
      } else if (value != this.ruleForm.password) {
        callback(new Error(this.$t('Register.matchPass')));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      ruleForm: {
        name: "",
        password: "",
        checkPassword: "",
        email: "",
      },
      rules: {
        name: [
          { required: true, message: this.$t('Register.blankAccount'), trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: this.$t('Register.errAccount'),
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: this.$t('Register.blankPassword'), trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: this.$t('Register.errPassword'),
            trigger: "blur",
          },
        ],
        checkPassword: [
          { required: true, message: this.$t('Register.checkPassword'), trigger: "blur" },
          {
            validator: validatePass,
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: this.$t('Register.blankEmail'), trigger: "blur" },
          {
            type: "email",
            message: this.$t('Register.errEmail'),
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          register(this.ruleForm)
            .then((value) => {
              const { code, message } = value;
              if (code === 200) {
                this.$message({
                  message: this.$t('Register.successUp'),
                  type: "success",
                });
                setTimeout(() => {
                  this.loading = false;
                  this.$router.push({ path: this.redirect || "/login" });
                }, 0.1 * 1000);
              } else {
                this.$message.error(this.$t('Register.failUp') + message);
              }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    reSetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
el-card {
  width: 200px;
}
</style>